/*
 * @Description: 拼团订单详情
 * @version: 1.0.0
 * @Company: ZMT
 * @Port: 余晖
 * @Author: XU BING
 * @Date: 2018-10-27 11:00:32
 * @LastEditors: XU BING
 * @LastEditTime: 2018-11-21 11:17:52
 */

var leftMilli = "",
  leftMillipt = "", //计时器
  app = {},
  dataInfo = new Object(),
  module = {
    init: function() {
      app = new Vue({
        el: '#app',
        data: {
          radio: is_weixn() ? '4' : '5', //4 - wx , 5 - zfb
          isWx: is_weixn(),
          show: false,
          time: "",
          timept: "",
          orderId: "",
          orderDetailData: {},
          goodsList: [],
          messageInfoData: [],
          aliPay: "", //支付宝支付
          wxInfo: {}, //微信支付
          userId: userId,
          groupon: {} //拼团状态
        },
        methods: {
          init: function() { //初始化vue方法
            app.orderId = getQueryString("orderId");
            app.getOrderDetail() //订单详情
            app.messageInfo();
            app.getOrderType()
          },
          PTdetail: function() { //跳转拼团详情
            location.href = "PtDetails.html?groupId=" + app.groupon.grouponOrderId + "&orderId=" + getQueryString("orderId")
          },
          leftTimer: function(leftMillisecond) { //时间计时
            var leftTime = leftMillisecond //计算剩余的毫秒数
            var hours = parseInt(leftTime / 1000 / 60 / 60 % 24, 10); //计算剩余的小时
            var minutes = parseInt(leftTime / 1000 / 60 % 60, 10); //计算剩余的分钟
            var seconds = parseInt(leftTime / 1000 % 60, 10); //计算剩余的秒数
            app.time = app.checkTime(hours) + ":" + app.checkTime(minutes) + ":" + app.checkTime(seconds);
            if (leftMillisecond <= 0) {
              app.time = "00:00:00";
              clearInterval(leftMilli)
            } else {
              clearInterval(leftMilli)
              leftMilli = setInterval(function() {
                app.leftTimer(leftMillisecond - 1000)
              }, 1000);
            }
          },
          timePT: function(leftMillisecond) {
            var leftTime = leftMillisecond //计算剩余的毫秒数
            var hours = parseInt(leftTime / 1000 / 60 / 60 % 24, 10); //计算剩余的小时
            var minutes = parseInt(leftTime / 1000 / 60 % 60, 10); //计算剩余的分钟
            var seconds = parseInt(leftTime / 1000 % 60, 10); //计算剩余的秒数
            app.timept = app.checkTime(hours) + ":" + app.checkTime(minutes) + ":" + app.checkTime(seconds);
            if (leftMillisecond <= 0) {
              app.timept = "00:00:00";
              clearInterval(leftMillipt)
            } else {
              clearInterval(leftMillipt)

              leftMillipt = setInterval(function() {
                app.timePT(leftMillisecond - 1000)
              }, 1000);
            }
          },
          checkTime(i) { //将0-9的数字前面加上0，例1变为01
            if (i < 10) {
              i = "0" + i;
            }
            return i;
          },
          subPhone(message) {
            if (message) {
              return message.substr(0, 3) + "****" + message.substr(7);
            }
          },
          pay: function() {
            app.show = true
          },
          close: function() {
            app.show = false
          },
          messageInfo: function() {
            ajaxTemplate(addressTest + SERVICE.messageInfo, function(data) {

              if (data.tag == 0) {
                app.order_info = data.data.result.order_info
                app.messageInfoData = data.data.result.shipping_info[0]
                $(".vue-loading").hide()
              } else {
                app.$toast(data.errMsg)
              };
            }, {
              orderId: app.orderId
            });
          },
          mounted: function(orderId) { //删除订单
            this.$dialog.confirm({
              message: '确认删除订单吗？'
            }).then(() => { //确认
              app.$toast.loading({
                forbidClick: true,
                loadingType: 'spinner',
                duration: 0,
              });
              ajaxTemplate(addressUrl + SERVICE.deleteOrder, function(data) {
                app.$toast.clear()
                if (data.tag == 0) {
                  location.href = "myOrder.html?orderStatus=0&active=0"
                } else {
                  app.$toast(data.errMsg)
                };
              }, {
                userId: userId,
                orderId: orderId
              });

            }).catch(() => { //取消
              console.log("取消")
            });
          },
          cancelOrder: function(orderId) { //取消订单
            this.$dialog.confirm({
              message: '确认取消订单吗？'
            }).then(() => { //确认
              app.$toast.loading({
                forbidClick: true,
                loadingType: 'spinner',
                duration: 0,
              });
              ajaxTemplate(addressUrl + SERVICE.cancelOrder, function(data) {
                app.$toast.clear()
                if (data.tag == 0) {
                  location.href = "myOrder.html?orderStatus=0&active=0"
                } else {
                  app.$toast(data.errMsg)
                };
              }, {
                userId: userId,
                orderId: orderId
              });
            }).catch(() => { //取消
              console.log("取消")
            });
          },
          confirmOrder: function(orderId) { //确认收货
            app.$toast.loading({
              forbidClick: true,
              loadingType: 'spinner',
              duration: 0,
            });
            ajaxTemplate(addressUrl + SERVICE.confirmOrder, function(data) {
              app.$toast.clear()
              if (data.tag == 0) {
                app.getOrderDetail();
              } else {
                app.$toast(data.errMsg)
              };
            }, {
              userId: userId,
              orderId: orderId
            });
          },
          getOrderDetail: function() { //订单详情
            ajaxTemplate(addressUrl + SERVICE.orderDetailGet, function(data) {
              if (data.tag == 0) {
                var timestamp = new Date().getTime()
                app.orderDetailData = data.data.result[0]
                app.goodsList = data.data.result[0].packList

                if (app.orderDetailData.orderStatus == 10) {
                  app.leftTimer(app.orderDetailData.endTime - timestamp)
                }
              } else {
                app.$toast(data.errMsg)
              };
            }, {
              userId: userId,
              orderId: app.orderId
            });
          },
          toPay: function() {
            app.$toast.loading({
              forbidClick: true,
              message: '数据提交中...',
              loadingType: 'spinner',
              duration: 0,
            });
            app.show = false;
            app.payInfo = {}
            app.payInfo.orderId = this.orderId;
            app.payInfo.ptType = 'fx';
            app.radio == '4' ? app.payInfo.channel = 'h5fxcwx' : app.payInfo.channel = 'h5fxcali'
            app.radio == '4' ? app.payInfo.openid = openId : app.payInfo.returnUrl = window.location.href.split('/html/')[0] + '/html/PtOrderDetails.html?orderId=' + this.orderId + '&tag=' + 1;
            if (!!this.isWx && (window.location.href.indexOf('mtmall.meetao.com') != 8)) { //微信支付跳转第三方
              //版本号1.1 没有拼团数据
              //1 - 普通支付（分销商品、大礼包）、 2 - 普通订单支付 、3 - 拼团支付 、 4 - 拼团订单支付
              // !!this.activityId ? dataInfo.payType = 3 : dataInfo.payType = 1;
              dataInfo.payType = 4;
              location.href = 'https://premtmall.meetao.com/html/WxPay.html?mallUrl=' + window.location.href.split('/html/')[0] + '&ajaxUrl=' + addressPayUrl + '&orderId=' + this.orderId + '&openid=' + app.payInfo.openid + '&payType=' + dataInfo.payType;
            } else {
              ajaxTemplate(addressPayUrl + SERVICE.payUrl, function(data) {
                /*app.$toast.clear();*/
                if (data.tag == 0 && app.radio == '4') { //wxPay
                  if (typeof WeixinJSBridge == "undefined") {
                    if (document.addEventListener) {
                      document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                    } else if (document.attachEvent) {
                      document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                      document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                    }
                  } else {
                    app.onBridgeReady(data.data.result);
                  }
                } else if (data.tag == 0 && app.radio == '5') { //alipay
                  if (getQueryString("tag") == 1) {
                    app.getOrderInfo()
                  }
                  app.aliPay = decodeURIComponent(data.data.result.sign.replace(/\+/g, " "));
                  setTimeout(function() {
                    document.forms[0].submit();
                  }, 250);
                } else {
                  app.$toast.clear();
                  app.$toast(data.errMsg);
                };
              }, app.payInfo);
            };
          },
          goApp: function() {
            app.$dialog.confirm({
              title: '提示',
              message: '请下载姿美汇APP进行申请退款'
            }).then(() => {

              downAppFun();
            }).catch(() => {

            });
          },
          getNum: function() {
            var Num = "";
            for (var i = 0; i < 32; i++) {
              Num += Math.floor(Math.random() * 10);
            }
            return Num;
          },
          onBridgeReady: function(msg) {
            WeixinJSBridge.invoke(
              'getBrandWCPayRequest', {
                "appId": msg.appId, //公众号名称，由商户传入
                "timeStamp": msg.timeStamp, //时间戳，自1970年以来的秒数
                "nonceStr": msg.nonceStr, //随机串
                "package": msg.package_str,
                "signType": "MD5", //微信签名方式：
                "paySign": msg.paySign //微信签名
              },
              function(res) {
                if (res.err_msg == "get_brand_wcpay_request:ok") {
                  app.getOrderInfo()
                  /*app.getOrderDetail() //订单详情*/
                  window.location.reload()
                } else {
                  window.location.reload()
                }
              });
          },
          newLogistics: function(orderId, packId) { //拆单物流信息
            // return 111

            ajaxTemplate(addressUrl + SERVICE.msgInfo, function(data) {

              if (data.tag == 0) {
                console.log(data.data.result.shipping_info[0].context)
                $("#" + packId).html(data.data.result.shipping_info[0].context)
                $("." + packId).html(data.data.result.shipping_info[0].time)

              } else {
                app.$toast(data.errMsg)
              };

            }, {
              orderId: orderId,
              packId: packId
            });
          },
          getOrderType: function() { //获取拼团状态
            ajaxTemplate(addressTest + SERVICE.PTorderDetail, function(data) {

              if (data.tag == 0) {

                app.groupon = data.data.result
                if (app.groupon.isGrouponOrder == "n" && app.orderDetailData.orderStatus != 10) {
                  $(".orderStatus").css("display", "none")
                }
                if (app.groupon.grouponStatus == 0) {

                  app.timePT(app.groupon.leftTime)
                }


              } else {
                app.$toast(data.errMsg)
              };

            }, {
              orderId: app.orderId
            });
          },
          getOrderInfo: function() { //获取订单数据
            ajaxTemplate(addressUrl + SERVICE.getOrderInfo, function(data) {
              log(data);
              if (data.tag == 0) {
                //	dataInfo.init.orderInfo = data.data.result[0];
              } else {
                dataInfo.init.$toast(data.errMsg);
              };
            }, {
              userId: userId,
              orderId: app.orderId
            });
          },



        }

      });
      app.init()


    }
  }
module.init()
